<template>
	<view class="page">
		<image src="/static/product.png" mode="widthFix"></image>
		<view class="foot foot_box">
			<view class="btn btn_save" @tap="buy">立即购买</view>
			<view class="btn btn_save" :class="[{'un_btn':!$data.can}]" @tap="take">领优惠券</view>
		</view>
		<PopupSuccess v-model:show="$data.successShow" />
	</view>
</template>

<script setup>
	import {
		reactive,
		ref,
		nextTick
	} from "vue";
	import {
		onLoad,
		onReady,
	} from "@dcloudio/uni-app";
	import {
		useSetRefreshTokenStorage,
		useSetTokenStorage
	} from "../../hooks/useStorage";
	import {
		couponWeb,
		getCdkCoupon
	} from "../../api/Interface/app";
	import errorCode from "../../utils/http/errorCode";
	import BasicConfig from "../../utils/config"
	import PopupSuccess from "../../components/PopupSuccess.vue";
	import {
		isDev
	} from "../../utils/env";
	const $data = reactive({
		can: false, // 可领取
		couponPackageId: "", // 券包id
		successShow: false, // 兑换成功
		domesticServiceId: "", // 日常保洁服务id
	})
	onLoad((options) => {
		console.log("options", options)
		if (isDev) {
			options.token =
				"Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJxbWciLCJhdWQiOiJxbWciLCJpYXQiOjE3NDAzNTY4MjIsIm5iZiI6MTc0MDM1Njg4MiwiZXhwIjoxNzQwNDI4ODIyLCJleHRlbmQiOnsiaWQiOjYsIm5pY2tuYW1lIjoiXHU1NDE1XHU2NTg3IiwiSXNzRG9tYWluIjoiaHR0cHM6Ly90ZXN0LnFtZ2RqLmNvbSIsIm1vZGUiOiJhcGkifX0.Y2LaCJEAIDejeQGmxGfBNO82heYpzLDxjpsHSZmiXEA";
			options.refreshToken =
				"Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJxbWciLCJhdWQiOiJxbWciLCJpYXQiOjE3NDAzNTY4MjIsIm5iZiI6MTc0MDM1Njg4MiwiZXhwIjoxNzQwOTYxNjIyLCJleHRlbmQiOnsiaWQiOjYsIm5pY2tuYW1lIjoiXHU1NDE1XHU2NTg3IiwiSXNzRG9tYWluIjoiaHR0cHM6Ly90ZXN0LnFtZ2RqLmNvbSIsIm1vZGUiOiJhcGkifX0.uo8kllajY8VnL2lHhGnzyj8nEtaFnCUZ_yUuJufGF4E"
		}
		const token = options.token;
		const refreshToken = options.refreshToken;
		useSetTokenStorage(token)
		useSetRefreshTokenStorage(refreshToken)
		init()
	})
	const init = () => {
		couponWeb().then(res => {
			console.log("couponWeb", couponWeb)
			if (res.code === errorCode.success) {
				$data.can = res.data.can_receive == 1; // 是否能领取优惠券 0.不能领取 1.能领取
				$data.couponPackageId = res.data.couponPackageId; // 券包id
				$data.domesticServiceId = res.data.domesticServiceId; // 日常保洁服务id
			}
		})
	}
	const buy = () => {
		console.log("立即购买");
		const id = $data.domesticServiceId;
		const APPID = "wxbdeff9ae3263ed50"
		const path = "package-service/pages/index";
		const env_version = "release";
		const query = `q=${id}`;
		const url = `weixin://dl/business/?appid=${APPID}&path=${path}&env_version=${env_version}&query=${query}`;
		location.href = url
		console.log("url", url);
		uni.redirectTo({
			url: "/package-service/pages/index?q=" + id,
			success(res) {
				console.log("redirectTo成功", res)
			},
			fail(err) {
				console.log("redirectTo失败功", err)
			}
		})
	}
	const take = () => {
		if (!$data.can) {
			return;
		}
		getCdkCoupon({
			type: 0,
			couponPackageId: $data.couponPackageId
		}).then(res => {
			BasicConfig.toast(res.msg)
			if (res.code === errorCode.success) {
				$data.can = false;
				$data.successShow = true;
				init()
			}
		});
	}
</script>

<style lang="scss" scoped>
	.page {
		width: 100vw;

		image {
			width: 100%;
		}

		.foot_box {
			background: transparent;

			.btn {
				border: 2px solid #ffffff;
				background: linear-gradient(#A6E97D, #00B07B);
			}

			.un_btn {
				background: #f0f0f0;
				color: #aaaaaa;
			}
		}
	}
</style>